package com.hex.ds.hdrs.calendar.service;

import cn.hutool.core.collection.CollectionUtil;
import com.hex.ds.hdrs.calendar.dao.SysConfigDao;
import com.hex.ds.hdrs.calendar.po.SysConfig;
import com.hex.ds.hdrs.common.constant.Const;
import com.hex.ds.hdrs.common.utils.HdrsDateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class SysConfigService {

    @Autowired
    private SysConfigDao sysConfigDao;

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void add(List<String> checkList) {
        SysConfig sysConfig = new SysConfig();
        String val = CollectionUtil.isEmpty(checkList) ? null : String.join(",", checkList);
        sysConfig.setId(HdrsDateUtil.getMillis(new Date()));
        sysConfig.setValue(val);
        sysConfig.setLabel(Const.SYS_CONFIG_LABEL);
        sysConfig.setSign(Const.SYS_CONFIG_SIGN);
        sysConfig.setCrtTs(HdrsDateUtil.getCurrentTime());
        this.sysConfigDao.add(sysConfig);
    }

    /**
     * 获取系统默认工作日配置值
     *
     * @return
     */
    public List<String> getSysConfigValue() {
        String sign = Const.SYS_CONFIG_SIGN;
        SysConfig sysConfig = this.sysConfigDao.getSysConfigValue(sign);
        if (sysConfig != null) {
            List<String> checkList = Arrays.asList(sysConfig.getValue().split(","));
            return checkList;
        }
        return new ArrayList<>();
    }

    public void edit(List<String> checkList) {
        log.debug("【进入了SysConfigService的edit】checkList：{}", checkList);
        String val = CollectionUtil.isEmpty(checkList) ? null : String.join(",", checkList);
        SysConfig sysConfig = new SysConfig();
        sysConfig.setValue(val);
        sysConfig.setSign(Const.SYS_CONFIG_SIGN);
        this.sysConfigDao.edit(sysConfig);
    }
}
